﻿Imports System.Data.SqlClient

Public Class frmSelectPolicy

    Public Sub FillPoliciesList()
        If Me.txtClientName.Text.Trim.Trim.Length = 0 Then
            MsgBox("Pleade enter a valid client ID")
        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand("Select * From Policies Where EndorsNo=N'' and ClientNo=" & CInt(Me.txtClientID.Text.Trim), cnn)
                Dim Reader As SqlDataReader

                Me.GridPolicies.Rows.Clear()

                cnn.Open()
                Reader = cmd.ExecuteReader
                While Reader.Read
                    Me.GridPolicies.Rows.Add(New String() {Reader.Item("SNo"), Reader.Item("InsurComp"), Reader.Item("ClassName"), Reader.Item("PolicyNo"), _
                                                           CDate(Reader.Item("PeriodFrom")).ToString("yyyy/MM/dd"), _
                                                            CDate(Reader.Item("PeriodTo")).ToString("yyyy/MM/dd")})
                End While
                cnn.Close()

                'Check Expiry Date
                For Each Row As DataGridViewRow In Me.GridPolicies.Rows
                    If Now.Date >= CType(Row.Cells(5).Value, Date).Date Then
                        Row.Cells(5).Style.BackColor = Color.Red
                    Else
                        Row.Cells(5).Style.BackColor = Color.White
                    End If
                Next

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub frmSelectPolicy_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SelPolInsurComp = ""
        SelPolClsOfBus = ""
        SelPolNo = ""
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub GridPolicies_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridPolicies.CellDoubleClick
        If e.RowIndex > -1 Then
            SelPolInsurComp = Me.GridPolicies.Rows(e.RowIndex).Cells(1).Value
            SelPolClsOfBus = Me.GridPolicies.Rows(e.RowIndex).Cells(2).Value
            SelPolNo = Me.GridPolicies.Rows(e.RowIndex).Cells(3).Value
            Me.Close()
        End If
    End Sub
End Class